package models

import (
	"gitee.com/sansaniot/ssiot-core/httpmvc/model"
	"gorm.io/datatypes"
	cDto "ssdevice/common/dto"
	"ssdevice/internal/device/models/query"
)

type SysReportForms struct {
	Id        string         `json:"id" gorm:"primaryKey;type:varchar(255);comment:id"`
	Name      string         `json:"name" gorm:"type:varchar(255);comment:报表名称"`
	UserId    string         `json:"userId" gorm:"type:varchar(255);comment:用户id"`
	QueryData datatypes.JSON `json:"queryData" gorm:"type:jsonb;comment:查询条件"`

	CreateTime *cDto.LocalTime `json:"createTime" gorm:"type:timestamptz(6);comment:创建时间"`
	model.Model
}

func (SysReportForms) TableName() string {
	return "sys_report_forms"
}

func (e *SysReportForms) Generate() interface{} {
	o := *e
	return &o
}

func (e *SysReportForms) GetId() interface{} {
	return e.Id
}

func (e *SysReportForms) FindReportFormsList(req *query.SysReportFormsQuery) (err error, result interface{}) {
	list := make([]SysReportForms, 0)
	var count int64
	if req.PageIndex > 0 && req.PageSize > 0 {
		err = e.FindPage(&list, &count,
			cDto.MakeCondition(req.GetNeedSearch()),
			cDto.OrderDest("create_time", true, req.OrderBy),
			cDto.Paginate(req.GetPageSize(), req.GetPageIndex()),
		)
		result = &cDto.Page{
			Records:   list,
			Total:     count,
			PageSize:  req.GetPageSize(),
			PageIndex: req.GetPageIndex(),
			Pages:     (count + int64(req.GetPageSize()) - 1) / int64(req.GetPageSize()),
			Other:     nil,
		}
	} else {
		err = e.FindList(&list,
			cDto.MakeCondition(req.GetNeedSearch()),
			cDto.OrderDest("create_time", true, req.OrderBy),
		)
		result = list
	}
	return
}
